﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class TutorialPointer : MonoBehaviour
{
    public enum pointType{
        normal,
        hold,
    }

    
    public GameObject normal_leftPointer;
    public GameObject hold_leftPointer;

    public GameObject normal_rightPointer;
    public GameObject hold_rightPointer;

    public GameObject normalPointer;
    public GameObject holdPointer;

    public pointType nowType
    {
        get {
            return _nowType;
        }
        set
        {
            if (_nowType == pointType.hold)
            {
                normalPointer.gameObject.SetActive(true);
                holdPointer.gameObject.SetActive(false);
            }
            else if (_nowType == pointType.normal)
            {
                holdPointer.gameObject.SetActive(true);
                normalPointer.gameObject.SetActive(false);
            }
            _nowType = value;
        }
    }
    public pointType _nowType = pointType.normal;
    public GuiClickTutorialNode.FingerType fingerType = GuiClickTutorialNode.FingerType.Left;

    public float speed = 0.3f;

    public List<RectTransform> effectList = new List<RectTransform>();
    int index = 0;

    public void StartPoint(GuiClickTutorialNode.FingerType type)
    {
        SetAllDisable();
        fingerType = type;
        switch (fingerType)
        {
            case GuiClickTutorialNode.FingerType.Left:
                normalPointer = normal_leftPointer;
                holdPointer = hold_leftPointer;
                break;
            case GuiClickTutorialNode.FingerType.Right:
                normalPointer = normal_rightPointer;
                holdPointer = hold_rightPointer;
                break;
            default:
                break;
        }
        nowType = pointType.normal;
        gameObject.SetActive(true);
    }

    public void SetAllDisable()
    { 
        normal_leftPointer.SetActive(false);
        hold_leftPointer.SetActive(false);

        normal_rightPointer.SetActive(false);
        hold_rightPointer.SetActive(false);
    }

    private void Awake()
    {
        StartEffect();
    }

    public void StartEffect()
    {
        index = 0;
        for (int i = 0; i < effectList.Count; i++)
        {
            effectList[i].localScale = new Vector3(0, 0, 1);
        }
        StartDoTween();
        Invoke( "StartDoTween", 1f);

    }

    private void StartDoTween()
    {
        StartEffect(effectList[index++]);
    }

    public void StartEffect(RectTransform trans)
    {
        trans.localScale = new Vector3(0, 0, 0);
        var twen = trans.DOScale(new Vector3(3, 3, 1), 3);
        twen.onComplete += () =>
        {
            StartEffect(trans);
        };
    }

    float deltaTime = 0;
    public void Update()
    {
        if(deltaTime >= speed)
        {
            ChangeState();
            deltaTime = 0;
            return;
        }
        deltaTime += Time.deltaTime;
    }

    public void ChangeState()
    {
        if(nowType == pointType.hold) { 
            nowType = pointType.normal;
        }
        else if(nowType == pointType.normal)
        {
            nowType = pointType.hold;
        }
    }

    public void Stop()
    {
        gameObject.SetActive(false);
    }
}

